<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\common\model\Cangku;

/**
 * 转盘接口
 */
class Zhuanpan extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        // $this->user_id = 2;
    } 
      /**
     * 转盘商品列表
     */
    public function list(){
      $return = Db::name('pan_good')->field("*,FROM_UNIXTIME(createtime,'%Y-%m-%d %H:%i:%s') as createtime")->select();
      $this->success('转盘商品列表',$return);
    } 
 
    /**
     * 转盘抽奖
     * @ApiSummary  ()
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/zhuanpan/choujiang)
     * @ApiReturn({
{
  "code": 0,
  "msg": "请转发获取转盘次数",
  "time": "1606121514",
  "data": null
}

     })
     */
    public function choujiang(){

      $today = "select * from `fa_share_num` Where  date_format(from_unixtime(createtime),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')";
      $today_data = Db::query($today);
      if(empty($today_data))$this->error('请转发获取转盘次数');
      if($today_data[0]['use_num']==3)$this->error('今天已达最大次数');
      if($today_data[0]['can_num']==0)$this->error('请转发获取转盘次数');

      $pan_good = Db::name('pan_good')->select();
      $count = count($pan_good);//转盘物品数量

      $out_num = mt_rand(0,$count-1);
      $out_good = $pan_good[$out_num];//得奖物品

      Db::name('share_num')->where('user_id',$this->user_id)->setInc('use_num');
      Db::name('share_num')->where('user_id',$this->user_id)->setDec('can_num');
      switch ($out_good['status']) {
        case 'zhongzi':
          $shop_data = Db::name('shop')->where('id',$out_good['shop_id'])->field('image,text,name,id')->find();

          $Cangku = new Cangku();
          $now_my_cangku = $Cangku->where(['shop_id'=>$out_good['shop_id'],'user_id'=>$this->user_id])->find();
                
          if($now_my_cangku){
            $now_my_cangku->setInc('num');
          }else{
            $insert_cangku['user_id'] = $this->user_id;
            $insert_cangku['shop_id'] = $out_good['shop_id'];
            $insert_cangku['image'] = $shop_data['image'];
            $insert_cangku['text'] = $shop_data['text'];
            $insert_cangku['name'] = $shop_data['name'];
            $insert_cangku['num'] = 1;
            $insert_cangku['status'] = 'zhongzi';
            $insert_cangku['updatetime'] = time(); 

            Db::name('cangku')->insert($insert_cangku);
          }  
          $return_text = '获得'.$out_good['name'];          
          break;

        case 'exp':
          Db::name('user')->where('id',$this->user_id)->setInc('experience',$out_good['exp']);
          $return_text = '获得经验值'.$out_good['exp'];
          
          $Index = new \app\api\controller\Index;
          $Index->up_action();
          break;
        //抵用卷
        case 'diyong':
            $insert_cangku['user_id'] = $this->user_id;
            $insert_cangku['pan_good_id'] = $out_good['id'];
            $insert_cangku['image'] = $out_good['img'];
            $insert_cangku['text'] = $out_good['text'];
            $insert_cangku['name'] = $out_good['name'];
            $insert_cangku['num'] = 1;
            $insert_cangku['status'] = 'diyong';
            $insert_cangku['updatetime'] = time(); 

            Db::name('cangku')->insert($insert_cangku);
            $return_text = '获得'.$out_good['name'];

          break;
        //获取话费
        case 'huafei':
          Db::name('user')->where('id',$this->user_id)->setInc('huafei_price',$out_good['huafei_price']);

          $insert_huafei_log['text'] = '抽奖获取'.$out_good['huafei_price'];
          $insert_huafei_log['user_id'] = $this->user_id;
          $insert_huafei_log['createtime'] = time();
          $insert_huafei_log['huafei_price'] = $out_good['huafei_price'];

          Db::name('huafei_log')->insert($insert_huafei_log);//话费获取记录

          $return_text = '获得'.$out_good['name'];
          break;
        
      }
      $this->success($return_text);
    } 

     /**
     * 分享获得转盘机会，每天可分享三次
     * @ApiSummary  ()
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/zhuanpan/share)
     * @ApiReturn({
{
  "code": 1,
  "msg": "分享成功增加50.00经验值",
  "time": "1606117357",
  "data": null
}
{
  "code": 0,
  "msg": "今天的分享达到上限",
  "time": "1606117294",
  "data": null
}
     })
     */

    public function share(){
      $nowdate = date('ymd',time());
      $if_exist = Db::name('share_num')->where(['user_id'=>$this->user_id])->find();
      if($if_exist){
        if(date('ymd',$if_exist['createtime'])==$nowdate){//当天的数据
          if($if_exist['share_num']==3)$this->error('今天的分享达到上限');
          Db::name('share_num')->where(['user_id'=>$this->user_id])->setInc('share_num');
          Db::name('share_num')->where(['user_id'=>$this->user_id])->setInc('can_num');
        }else{//不是当天就修改为新日期
          Db::name('share_num')->where(['user_id'=>$this->user_id])->update(['createtime'=>time(),'share_num'=>1,'use_num'=>0]);
          Db::name('share_num')->where(['user_id'=>$this->user_id])->setInc('can_num');
        }
      }else{
        $insert['user_id'] = $this->user_id;
        $insert['share_num'] = 1;
        $insert['use_num'] = 0;
        $insert['can_num'] = 1;
        $insert['createtime'] = time();
        Db::name('share_num')->insert($insert);
      }
      $add_exp = Db::name('new_prop')->where('id',6)->value('add_exp');
      Db::name('user')->where('id',$this->user_id)->setInc('experience',$add_exp);
      $Index = new \app\api\controller\Index;
      $res = $Index->up_action();
      $this->success('分享成功增加'.$add_exp.'经验值');
    }


  

    

  

}
